﻿using QD.PushManager.DB;
using QD.PushManager.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QD.PushManager.Manager
{
    /// <summary>
    /// 订阅宝产品操作
    /// </summary>
    public static class ProductManager
    {
        static string TableName { get { return "sql_s003.dbo.vIFO_Product"; } }

        public static int Add(Product model)
        {
            using (var db = new ShtDatabase())
            {
                return (int)db.Insert(model);
            }
        }

        public static int Update(Product model)
        {
            using (var db = new ShtDatabase())
            {
                return (int)db.Update(model);
            }
        }

        /// <summary>
        /// 更新现有商品的标题、图片
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int UpdateByPush(Product model)
        {
            using (var db = new ShtDatabase())
            {
                return db.Execute(string.Format("update {0} set deleted=0,Title='{1}',PicUrl='{2}' where ProductId={3}", TableName, model.Title, model.PicUrl, model.ProductId));
                //return db.Execute("update @0 set Title=@1,PicUrl=@2 where ProductId=@3", TableName, model.Title, model.PicUrl, model.ProductId);
            }
        }
        
        /// <summary>
        /// 获取推送的产品
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="categoryId"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<Product> GetPushProduct(int userId, int categoryId, int pageSize = 8)
        {
            if (userId < 1) return null;

            var sb = new StringBuilder();
            sb.AppendFormat(" select top {0} ProductID,0 as articleid, UserID,Title,Keywords,TemplateID,CategoryID,Tags,EditorID,PicUrl,CopyFrom,EditorName,Tag,MaxCharPerPage,ColumnID,ProductNum", pageSize);
            sb.Append(", '' as Details, ROW_NUMBER() over(order by isnull(CategoryID, 0) desc, isnull(Tags, 0) asc, isnull(EditorID, 0) desc, Updated desc) as row_num");
            sb.AppendFormat(" from sql_s003.dbo.{0}", TableName);
            sb.Append(" where Deleted = 0 and ShowCommentLink = 0");
            sb.AppendFormat(" and userid = {0}", userId);
            sb.Append(" and datediff(DD, isnull(snapshottime3, '1970-01-01'), GETDATE()) > -1"); //有效期开始
            sb.Append(" and datediff(DD, GETDATE(), isnull(snapshottime4, '2099-01-01')) > -1"); //有效期截止
            if (categoryId > 0)
                sb.Append(" and isnull(CategoryID, 0) > 0");    //即时分类信息
            else
                sb.Append(" and isnull(CategoryID,0)< 1");
            sb.Append(" order by CategoryID desc,Tags asc,EditorID desc,Updated desc");  //排序：产品分类 > 推送次数 > 指定排序

            using (var db = new ShtDatabase())
            {
                return db.Fetch<Product>(sb.ToString());
            }
        }

    }
}